// FileSplit.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//#include<vcclr.h>
#include<istream>
#include<stdio.h>
#include<iostream>
#include<string>
#include<sys/stat.h>
//#include <unistd.h>
#include<fcntl.h>
using namespace std;

long get_filesize(FILE *);



int _tmain(int argc, _TCHAR* argv[])
{
	
	string filepath;
	string filename;

	char* absolute_fileidentifier;
	
	long size_org_file;
	long size_current_split_file;
    
    long size_pending;

	long split_files_size;
    
    int total_split_files = 0;

	cout << "Enter absolute file path (e.g. C:/novels/Robin Sharma) \n";
	getline(cin, filepath);

	cout << "Enter file name  with extension (e.g. Simplify Your Career.doc) available at path entered above \n";
	getline(cin, filename);

	filename = filepath + '/' + filename  ;
	cout << "absolute path entered is " << filename << "\n" ;

	absolute_fileidentifier = (char*)malloc( sizeof( char ) *(filename.length() + 1)  );
	strcpy( absolute_fileidentifier, filename.c_str() );
    
	//cout << "Abs path " << absolute_fileidentifier << "\n" ;
    FILE *hfile;
    
	//fopen_s( &hfile, "C:/SCDL/luck.txt", "rb");
	fopen_s( &hfile, absolute_fileidentifier, "rb");
    if(hfile == NULL)
    {
        cout << " No file found at described path" <<endl;
        
    }
    else
    {
        size_org_file = get_filesize(hfile);
        cout<<"size of org file in bytes is "<<size_org_file<<endl;

		cout <<"Enter split size in bytes" << "\n";
		cin >> split_files_size;
    } 
    

    size_pending = size_org_file;
   long size_pending_backup;    
    
    while(size_pending > 0 )
    {
       char * iterator;

	   char* file_type = ".txt";
		string split_file;
		char* name_split_file;
        
		char* temp_buffer;
		total_split_files++;
        
		iterator = (char*)malloc(sizeof(char) * 5);

		itoa(total_split_files, iterator, 10);

        split_file = filename + string(iterator);
        
		name_split_file = (char*)malloc( sizeof( char ) *(split_file.length() + 1)  );
	    strcpy( name_split_file, split_file.c_str() );

		temp_buffer = (char*) malloc(sizeof(char)*(split_files_size + 1)  );

		//All split files are of .txt type
		strcat(name_split_file, file_type);
    
		cout <<name_split_file<<endl;

        FILE* split_file_ptr;
        
       //fgets(temp_buffer, split_files_size, hfile); 
        fread(temp_buffer, 1, split_files_size, hfile);

        fopen_s( &split_file_ptr, name_split_file, "wb");
        
        if(split_file_ptr == NULL)
        {
                      
            cout << "Unable to create split file"<<endl;              
        }
                      
       else
       {                             
        
		   
		size_current_split_file = get_filesize(split_file_ptr);
        
		rewind(split_file_ptr);

       size_pending_backup = size_pending;


		if(size_pending >= split_files_size)
		   {
			   //fputs(temp_buffer, split_file_ptr);
               fwrite(temp_buffer, 1, split_files_size, split_file_ptr);
		   }
		   else
		   {
              if(size_pending > 0)
			  {
			   fwrite(temp_buffer, 1, size_pending, split_file_ptr);
			  }
			  else
			  {
               fwrite(temp_buffer, 1, size_pending_backup, split_file_ptr);
			  
			  }

		   }

        size_pending -= split_files_size;

		fclose(split_file_ptr);
       
		//cout << "Pending file size for copying is "<<size_pending<<endl;
       }                       
   }
                                                              
    
    fclose(hfile);

	char response;
	cout <<"Enter any alpha-numeric key to terminate"<<endl;
	cin >>response;
       
	return 0;
}


 long get_filesize( FILE *hfile)
                  {
                       long file_size_bytes;
                       fseek( hfile, 0L, SEEK_END);
                       file_size_bytes = ftell(hfile);
                       fseek(hfile, 0L, SEEK_SET);
					   rewind(hfile);
                       
                       return file_size_bytes;
                       }
                       
